<template>
  <div class="app-container">
    <el-form size="mini" :inline="true" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-button size="mini" @click="addOrUpdateHandle()" type="primary" v-if="Jurisdictionlist.indexOf('Add') > -1">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" size="mini" style="width: 100%; margin-bottom: 20px" row-key="id" v-loading="dataListLoading" :header-cell-style="{ background: '#fafafa', color: 'rgba(0,0,0,.85)' }" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
      <el-table-column prop="title" label="菜单名称" width="180"> </el-table-column>
      <el-table-column prop="level" label="菜单类型" width="180">
        <template slot-scope="scope">
          {{ scope.row.level === 1 ? '目录' : scope.row.level === 2 ? '菜单' : scope.row.level === 21 ? '菜单' : scope.row.level === 3 ? '按钮' : '' }}
        </template>
      </el-table-column>
      <el-table-column prop="icon" label="icon"> </el-table-column>
      <el-table-column prop="name" label="组件"> </el-table-column>
      <el-table-column prop="path_p" label="路径"> </el-table-column>
      <el-table-column prop="sort" label="排序"> </el-table-column>
      <el-table-column align="center" fixed="right" header-align="center" label="操作" width="200">
        <template slot-scope="scope">
          <el-button size="mini" @click="addOrUpdateHandle(scope.row)" plain type="primary" v-if="Jurisdictionlist.indexOf('Edit') > -1">编辑</el-button>
          <el-button size="mini" @click="handleDel(scope.row)" plain type="danger" v-if="Jurisdictionlist.indexOf('Delete') > -1">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update @refreshList="getDataList" ref="addOrUpdate"></add-or-update>
    <!-- 删除前通过发送验证码检验身份，此为验证码弹框 -->
  </div>
</template>
<script>
import AddOrUpdate from './add-or-update'
import city from './city.json'
import { permissionListApi, delUserApi, delPermissionApi } from '@/api/system/index.js'
import { roleAllApi } from '../../../api/commonApi'
var app
export default {
  beforeCreate() {
    app = this
  },
  filters: {
    levelDicFilter(value) {
      var return_name
      app.allRoleArr.forEach(function (element) {
        if (element.id == value) {
          return_name = element.name
        }
      })
      return return_name
    },
  },
  data() {
    return {
      allRoleArr: [],
      delrow: [], // 删除行信息
      dialogFormVisible: false, // 修改密码弹框控制
      timer: undefined, // 倒计时全局删除
      isNext: false,
      time: 90, // 发送短信时间,倒计时90s
      rules: {
        code: [{ required: true, message: '请输入验证码', trigger: 'blur' }],
      },
      dataForm: {
        code: undefined,
      },
      Jurisdictionlist: [], // 权限列表
      dataList: [],
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      roleOptions: [],
    }
  },
  components: { AddOrUpdate },
  mounted() {
    this.getAllRoleArr()
    this.getDataList()
    // 处理权限
    this.Jurisdictionlist = JSON.parse(localStorage.getItem('jurisdiction'))[this.$route.path] || []
  },
  computed: {},
  methods: {
    getAllRoleArr() {
      roleAllApi({}).then((res) => {
        if (res.code == 200) {
          this.allRoleArr = res.result
        }
      })
    },
    handleDel(row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning',
      })
        .then(() => {
          let para = { id: row.id }
          delPermissionApi(para).then((res) => {
            if (res.code == 200) {
              this.$message.success('删除成功')
              this.getDataList()
            }
          })
        })
        .catch(() => {})
    },

    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      const info = Object.assign({ pageNumber: this.pageIndex, pageSize: this.pageSize })
      permissionListApi(info)
        .then((res) => {
          this.dataList = res.data
          this.dataListLoading = false
        })
        .catch((e) => {
          this.dataList = []
          this.dataListLoading = false
        })
    },
    // 多选
    selectionChangeHandle(val) {},
    // 新增 / 修改
    addOrUpdateHandle(row) {
      this.$refs.addOrUpdate.open(row, this.allRoleArr)
    },
  },
}
</script>

<style lang="scss" scoped>
.my-btn .el-form-item__content {
  width: 100%;
}
.getCode {
  width: 100px;
  height: 40px;
  line-height: 40px;
  text-align: center;
  display: inline-block;
  color: #fff;
  background-color: #409eff;
  cursor: pointer;
  box-sizing: border-box;
  font-size: 14px;
  border-radius: 4px;
  margin-left: 20px;
}
</style>
